package com.lzy.cloud.permission.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.permission.dtos.PermissionDto;
import com.lzy.cloud.permission.facade.PermissionFacade;
import com.lzy.cloud.permission.request.PermissionRequest;
import com.lzy.cloud.permission.response.PermissionResponse;
import com.lzy.cloud.permission.response.RoleResponse;
import com.lzy.cloud.permission.service.PermissionService;
import com.lzy.framework.component.PageBean;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by WIN7 on 2018/5/16.
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @Autowired
    PermissionFacade permissionFacade;

    /**
     * 新增权限
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public Result addPermission(@RequestBody(required = false) PermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = permissionService.insert(DozerUtils.map(request, PermissionDto.class));
        Ensure.that(ok).isTrue("00040001");
        return new Result(ok);
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/{id}")
    public Result delPermission(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010001");
        Boolean ok = permissionFacade.delPermission(id);
        Ensure.that(ok).isTrue("00040002");
        return new Result(ok);
    }

    /**
     * 更新权限
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update")
    public Result updatePermission(@RequestBody PermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = permissionService.update(DozerUtils.map(request, PermissionDto.class));
        Ensure.that(ok).isTrue("00040003");
        return new Result(ok);
    }

    /**
     * 分页查询权限列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public Result selectList(PermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<PermissionDto> pageInfo = permissionService.selectList(pager, DozerUtils.map(request, PermissionDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), PermissionResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 权限详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}")
    public Result selectById(@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010001");
        PermissionDto permissionDto = permissionService.selectById(id);
        Ensure.that(permissionDto).isNotNull("00040004");
        return new Result(DozerUtils.map(permissionDto, PermissionResponse.class));
    }

    /**
     * 根据条件查询权限详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/detail")
    public Result selectPermission(@RequestBody PermissionRequest request) {
        Ensure.that(request).isNotNull("00010001");
        PermissionDto permissionDto = permissionService.selectPermission(DozerUtils.map(request, PermissionDto.class));
        Ensure.that(permissionDto).isNotNull("00040004");
        return new Result(DozerUtils.map(permissionDto, PermissionResponse.class));
    }
}
